'use strict';

const path = require('path');
const webpack = require('webpack');
const utils = require('../utils');
const nodeExternals = require('webpack-node-externals');
const ErrorOverlayPlugin = require('error-overlay-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const VueSSRServerPlugin = require('vue-server-renderer/server-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = function (data, config) {
  return {
    entry: {
      main: utils.resolve('template/_ssr.tsx'),
    },
    output: {
      // publicPath: '/static/' + data.moduleName + '/server',
      path: path.join(data.buildPath, 'server'),
      filename: '[name].js',
      chunkFilename: '[name].js',
      libraryTarget: 'commonjs2',
    },
    target: 'node',
    mode: 'development',
    devtool: 'cheap-module-source-map',
    stats: { children: false },
    resolve: utils.getResolve(data, config),
    resolveLoader: utils.getResolveLoader(data),
    externals: nodeExternals({
      whitelist: [/\.(?!(?:tsx?|json|css|jsx?)$).{1,5}$/i],
    }),
    module: {
      noParse: /^(vue|vue-router|vuex|vuex-router-sync)$/,
      rules: [
        utils.getVueLoader(),
        utils.getImageLoader(),
        utils.getLessLoader(true, config),
        utils.getCssLoader(true, config),
        utils.getMediaLoader(),
        utils.getFontsLoader(),
        utils.getSvgLoader(),
        ...utils.getTsLoader(data),
      ],
    },
    plugins: [
      new CleanWebpackPlugin(),
      new VueLoaderPlugin(),
      new VueSSRServerPlugin(),
      new ErrorOverlayPlugin(),
      new webpack.DefinePlugin({
        __isBrowser__: false,
      }),
      new webpack.LoaderOptionsPlugin({
        debug: true,
        minimize: false,
        options: {
          context: data.mainPath,
        },
      }),
      new webpack.LoaderOptionsPlugin({
        debug: true,
        minimize: false,
        options: {
          context: data.mainPath,
        },
      }),
    ],
  };
};
